<?php
error_reporting( E_ALL&~E_NOTICE );
ini_set( 'display_errors', 'off' );//屏蔽错误和警告提示
include("../mysql.php");

function dosql($tb_name,$mykey,$myvalue){
$check="SELECT * FROM `$tb_name` WHERE `mykey` = '$mykey' AND `myvalue` = '$myvalue'";
$ret=mysql_query($check);
$row=mysql_fetch_array($ret);
if($row["myvalue"]){
    echo "操作失败，已经有该条数据了";
    exit;
}
$check="SELECT * FROM `$tb_name` WHERE `mykey` = '$mykey'";
$ret=mysql_query($check);
$row=mysql_fetch_array($ret);
if($row["myvalue"]){
    $old_myvalue=$row["myvalue"];
    if($myvalue!=$old_myvalue){
        $mysql="UPDATE `$tb_name` SET `myvalue` = '$myvalue' WHERE `mykey` ='$mykey' and `myvalue` ='$old_myvalue';";
        mysql_query($mysql);
    }
    exit;
}
$sql="alter table `$tb_name` AUTO_INCREMENT=1";
mysql_query($sql);  //序列号自动归1
$mysql="INSERT INTO `$tb_name` (`id`, `mykey`, `myvalue`) values (NULL, '$mykey', '$myvalue');";
mysql_query($mysql);
echo "新增成功一条数据为:[$mykey,$myvalue]";
}

function doxuanhong($tb_name,$mykey,$myvalue){
$map=$_POST['map'];
$npcname=$_POST['npcname'];    

$check="SELECT * FROM `$tb_name` WHERE `map` = '$map' AND `npcname` = '$npcname' AND `mykey` = '$mykey' AND `myvalue` = '$myvalue'";
$ret=mysql_query($check);
$row=mysql_fetch_array($ret);
if($row["map"]){
    echo "操作失败，已经有该条数据了";
    exit;
}
$check="SELECT * FROM `$tb_name` WHERE `map` = '$map' AND `npcname` = '$npcname' AND `myvalue` = '$myvalue'";
$ret=mysql_query($check);
$row=mysql_fetch_array($ret);
if($row["map"]){
    $old_mykey=$row["mykey"];
    $old_myvalue=$row["myvalue"];
    if($mykey!=$old_mykey){
        $mysql="UPDATE `$tb_name` SET `mykey` = '$mykey' WHERE `mykey` ='$old_mykey' and `myvalue` ='$old_myvalue';";
        mysql_query($mysql);
    }
    if($myvalue!=$old_myvalue){
        $mysql="UPDATE `$tb_name` SET `myvalue` = '$myvalue' WHERE `mykey` ='$mykey' and `myvalue` ='$old_myvalue';";
        mysql_query($mysql);
    }
    exit;
}
$sql="alter table `$tb_name` AUTO_INCREMENT=1";
mysql_query($sql);  //序列号自动归1
$mysql="INSERT INTO `$tb_name` (`id`, `map`, `npcname`, `mykey`, `myvalue`) values (NULL, '$map', '$npcname', '$mykey', '$myvalue');";
//$mysql="INSERT INTO `eyuyan`.`$tb_name` (`id`, `map`, `npcname`, `mykey`, `myvalue`) values (NULL, '得到的589', '得到的', '豆豆564', null)";
mysql_query($mysql);
echo "新增成功一条数据为:[$map,$npcname,$mykey,$myvalue]";
}


function domap($tb_name,$mykey,$myvalue){//地图和秘境地图
$map=$_POST['map'];
 
$check="SELECT * FROM `$tb_name` WHERE `map` = '$map' AND `mykey` = '$mykey' AND `myvalue` = '$myvalue'";
$ret=mysql_query($check);
$row=mysql_fetch_array($ret);
if($row["map"]){
    echo "操作失败，已经有该条数据了";
    exit;
}
$check="SELECT * FROM `$tb_name` WHERE `map` = '$map' AND `myvalue` = '$myvalue'";
$ret=mysql_query($check);
$row=mysql_fetch_array($ret);
if($row["map"]){
    $old_mykey=$row["mykey"];
    $old_myvalue=$row["myvalue"];
    if($mykey!=$old_mykey){
        $mysql="UPDATE `$tb_name` SET `mykey` = '$mykey' WHERE `mykey` ='$old_mykey' and `myvalue` ='$old_myvalue';";
        mysql_query($mysql);
    }
    if($myvalue!=$old_myvalue){
        $mysql="UPDATE `$tb_name` SET `myvalue` = '$myvalue' WHERE `mykey` ='$mykey' and `myvalue` ='$old_myvalue';";
        mysql_query($mysql);
    }
    exit;
}
$sql="alter table `$tb_name` AUTO_INCREMENT=1";
mysql_query($sql);  //序列号自动归1
$mysql="INSERT INTO `$tb_name` (`id`, `map`, `mykey`, `myvalue`) values (NULL, '$map', '$mykey', '$myvalue');";
//$mysql="INSERT INTO `eyuyan`.`$tb_name` (`id`, `map`, `npcname`, `mykey`, `myvalue`) values (NULL, '得到的589', '得到的', '豆豆564', null)";
echo $mysql;
mysql_query($mysql);
echo "\n新增成功一条数据为:[$map,$mykey,$myvalue]"; 
}


//悬红 mykey描述 myvalue路径 额外 npcname npc名字  map 大地图名字
//地图 mykey 详细地图名 myvalue路径 额外  map 大地图名字
//秘境地图 mykey 详细地图名 myvalue路径 额外  map 大地图名字  前台uploadjs解析规则不同

function tellsql($type){// 非mykey-myvalue类型有悬红，地图，秘境地图，
$mykey=$_POST['mykey'];
$myvalue=$_POST['myvalue'];
echo "开始提交:$type  ";
switch ($type) {
    case "悬红":
doxuanhong("xuanhongs",$mykey,$myvalue);
     break;
    case "技能":
dosql("skills",$mykey,$myvalue);
     break;
    case "装备":
dosql("equips",$mykey,$myvalue);
     break;
    case "灯谜":
dosql("dengmis",$mykey,$myvalue);
     break;
    case "答题":
dosql("questions",$mykey,$myvalue);
     break;
    case "挖宝":
dosql("wabaos",$mykey,$myvalue);
     break;
    case "秘境":
domap("mjmaps",$mykey,$myvalue);
     break;
    case "江湖":
dosql("jhlists",$mykey,$myvalue);
     break;
    case "地图":
domap("maps",$mykey,$myvalue);
     break;
    case "物品":
dosql("things",$mykey,$myvalue);
     break;
   default:
     echo "$type为不合法的提交!";
     exit;
}  
}
if(isset($_POST['type']) and isset($_POST['mykey']) and isset($_POST['myvalue']) and isset($_POST['adminpwd']))
{
if($_POST['adminpwd']!="dashen"){
    //echo "无权操作";
    exit;
}
$type=$_POST['type'];    
tellsql($type);
}
else{
    echo "无权访问";
}
?>